package com.qlslylq.ad_x_sdk;

import android.app.Application;

import androidx.annotation.NonNull;

import com.qlslylq.ad.sdk.application.AdDesignSDK;
import com.qlslylq.ad.sdk.application.AdDesignSDKConfig;
import com.qlslylq.ad.sdk.core.listener.InterstitialAdListener;
import com.qlslylq.ad.sdk.core.listener.RewardAdListener;
import com.qlslylq.ad.sdk.core.loader.InterstitialAdLoader;
import com.qlslylq.ad.sdk.core.loader.RewardAdLoader;
import com.qlslylq.ad.sdk.core.rsp.AdError;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.interfaces.InitCallback;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad_x_sdk.comm.CommKey;
import com.qlslylq.ad_x_sdk.comm.CommRsp;
import com.qlslylq.ad_x_sdk.factory.BannerAdViewFactory;
import com.qlslylq.ad_x_sdk.factory.SplashAdViewFactory;
import com.qlslylq.ad_x_sdk.plugin.BasePlugin;

import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

/**
 * created by qlslylq <br/>
 * on 2022/5/23 <br/>
 */
public class AdXSdkPlugin extends BasePlugin {

    protected RewardAdLoader rewardAdLoader;
    protected InterstitialAdLoader interstitialAdLoader;

    public static void registerWith(PluginRegistry.Registrar registrar) {
        context = registrar.activity();

        AdXSdkPlugin plugin = new AdXSdkPlugin();
        plugin.onAttachedToEngine((Application) registrar.context().getApplicationContext(), registrar.messenger());

        registrar
                .platformViewRegistry()
                .registerViewFactory(
                        "com.qlslylq.ad-x-sdk/splashAdView",
                        new SplashAdViewFactory(registrar.activeContext(), registrar.messenger()));
        registrar
                .platformViewRegistry()
                .registerViewFactory(
                        "com.qlslylq.ad-x-sdk/bannerAdView",
                        new BannerAdViewFactory(registrar.activeContext(), registrar.messenger()));
    }

    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        Map<String, Object> req = (Map<String, Object>) call.arguments;

        if (call.method.equals("initSDK")) {
            initSDK(req);
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("initRewardAdLoader")) {
            initRewardAdLoader(req);
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("loadRewardAd")) {
            rewardAdLoader.load();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("showRewardAd")) {
            rewardAdLoader.show();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("loadAndShowRewardAd")) {
            rewardAdLoader.loadAndShow();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("initInterstitialAdLoader")) {
            initInterstitialAdLoader(req);
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("loadInterstitialAd")) {
            interstitialAdLoader.load();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("showInterstitialAd")) {
            interstitialAdLoader.show();
            result.success(CommRsp.newInstance().putSuccess());
        } else if (call.method.equals("loadAndShowInterstitialAd")) {
            interstitialAdLoader.loadAndShow();
            result.success(CommRsp.newInstance().putSuccess());
        } else {
            result.notImplemented();
        }
    }

    private void initSDK(Map<String, Object> req) {
        AdDesignSDKConfig config = GsonUtils.gsonToBean(GsonUtils.gsonString(req), AdDesignSDKConfig.class);
        AdDesignSDK.init(app, config, new InitCallback() {
            public void success() {
                CommRsp rsp = CommRsp.newInstance().putSuccess();
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void fail(AdPlatformEnum platform, int code, String msg) {
                CommRsp rsp = CommRsp.newInstance().putInitFail(platform, null, code, msg);
                BasePlugin.sendSuccessEvent(rsp);
            }
        });
    }

    private void initRewardAdLoader(Map<String, Object> req) {
        Integer posId = (Integer) req.get(CommKey.JH_REWARD_POS_ID);
        if (posId == null) {
            posId = 0;
        }
        rewardAdLoader = new RewardAdLoader(context, posId, new RewardAdListener() {
            public void onAdShow() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdShow");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdShowError(AdError error) {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdShowError").putError(error);
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onReward() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onReward");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdComplete() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdComplete");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdDismiss() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdDismiss");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdClick() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdClick");
                BasePlugin.sendSuccessEvent(rsp);
            }
        });
    }

    private void initInterstitialAdLoader(Map<String, Object> req) {
        Integer posId = (Integer) req.get(CommKey.JH_INTERSTITIAL_POS_ID);
        Boolean autoPlayMuted = (Boolean) req.get(CommKey.AUTO_PLAY_MUTED);
        if (posId == null) {
            posId = 0;
        }
        if (autoPlayMuted == null) {
            autoPlayMuted = true;
        }
        interstitialAdLoader = new InterstitialAdLoader(context, posId, autoPlayMuted, new InterstitialAdListener() {
            public void onAdShow() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdShow");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdShowError(AdError error) {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdShowError").putError(error);
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdComplete() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdComplete");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onDislikeShow() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onDislikeShow");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onDislikeSelected(String value) {
                CommRsp rsp = CommRsp.newInstance().putEventName("onDislikeSelected").putDislikeValue(value);
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onDislikeDismiss() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onDislikeDismiss");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdDismiss() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdDismiss");
                BasePlugin.sendSuccessEvent(rsp);
            }

            public void onAdClick() {
                CommRsp rsp = CommRsp.newInstance().putEventName("onAdClick");
                BasePlugin.sendSuccessEvent(rsp);
            }
        });
    }

}
